<template>
	<view>


		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel"
			@AddModuleData_select='AddModuleData_select'>
			<template #workbench>

				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="10">
					<template #BtnContent>
						<u-icon name="scan" color="#fff" size="25px" @click="scan"></u-icon>
					</template>
				</m-drag-button>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="60">
					<template #BtnContent>
						<u-icon name="plus" color="#fff" size="25px" @click="add"></u-icon>
					</template>
				</m-drag-button>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="110">
					<template #BtnContent>
						<u-icon name="search" color="#fff" size="25px" @click="search"></u-icon>
					</template>
				</m-drag-button>


			</template>
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>

								<th width='20%'>点检日期</th>
								<th width='25%'>点检单号</th>
								<th width='20%'>点检设备</th>
								<th width='10%'>点检人</th>
								<th width='5%'>周期</th>
							</tr>
						</table>
					</view>
					<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" class="z_paging">

						<view class="table">
							<table>

								<tbody>
									<tr style="border: 1px solid #ccc;" v-for="(item,index) in dataList" :key="index"
										@click="itemClick(item,index)" :class="{ hovered: hoveredIndex === index }">
										<td width='20%'>
											<view class="newlineDiv"><text>{{item.BillDate}}</text></view>
										</td>
										<td width='25%'>
											<view class="newlineDiv"><text>{{item.BillNo}}</text></view>
										</td>
										<td width='20%'>
											<view class="newlineDiv"><text>{{item.EquipmentName}}</text></view>
										</td>
										<td width='10%'>
											<view class="newlineDiv"><text>{{item.CheckupMan}}</text></view>
										</td>
										<td width='5%'>
											<view class="newlineDiv"><text>{{item.PeriodTypeName}}</text></view>
										</td>
									</tr>
								</tbody>
							</table>
						</view>
					</z-paging>
				</view>
				<!-- 	<view class="add">
					<u-icon name="plus" color="#fff" size="30" @click="add"></u-icon>
				</view>
				<view class="search">
					<u-icon name="search" color="#fff" size="30" @click="search"></u-icon>
				</view> -->

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0"
					@change="changePopup">
					<view class="popup_content">

						<u-cell-group :border='false'>


							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>

						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>
				<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false'
					@confirm='pickerConfirm'></u-datetime-picker>



			</template>

		</st-mc-framework>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>


	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';
	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {


				PeriodTypeList: [],
				ModuleTitle: '', //标题名称
				showMLoading: false, //是否加载
				loadingName: '正在加载中……', //加载名称
				BtnShow: false, //按钮显示
				FWorkshopID: 0,
				dataList: [],
				APPFunID: 0,
				tabIndex: 0,
				showLeft: false,
				FormData: {
					EquipmentID: 0,
					EquipmentName: '全部',
					PageIndex: 1,
					PageSize: 30,
					FBDate: '',
					FEDate: '',
				},
				TimeName: '',
				show: false,
				value1: Number(new Date()),
				hoveredIndex: null,
				disabled: false,
			}

		},
		onShow() {
			var that = this
			uni.$on('refreshList', () => {
				this.getList(); // 调用目标方法
			});
		},
		onUnload() {
			uni.$off('refreshList');
		},
		onReady() {
			var that = this
			uni.hideLoading();
			that.showMLoading = true
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;


			that.FWorkshopID = MesBasicInfo.FWorkshopID;
			that.ModuleTitle = appConfigInfo.ActiveModule.ModuleName


			that.$nextTick(() => {
				setTimeout(() => {
					that.BtnShow = true;
				}, 500)
			})
			that.GetMJPeriod()
		},
		onLoad(options) {
			var that = this
			that.$safeStatus({
				payload: '其他', // 要传递的数据
			});
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			if (options.Dispatchitem) {
				const dispatchItem = JSON.parse(options.Dispatchitem);
				that.assignFormData(that.FormData, dispatchItem);
				return;
			}
			const formDefaults = {
				EquipmentID: MesBasicInfo.warehouse.EquipmentID,
				EquipmentName: MesBasicInfo.warehouse.EquipmentName,
				FBDate: getBefore7DateTime(),
				FEDate: getFormatDateTime(new Date()),
			};
			that.assignFormData(that.FormData, formDefaults);
		},

		methods: {
			scan() {
				var that = this
				this.$scanPopup.openScan({
					success: (res) => {
						console.log(res);
						var jobid = res.result.replace(/^.*?\|/, '')
						console.log(jobid);
						that.Request.request('getEquipment', {
							FWorkshopID: that.FWorkshopID
						}, (ret) => {
							console.log(ret);
							if (ret.status == 200) {

								var Detailsitem = ret.data.ds.find(item => item.EquipmentID == jobid)
								console.log(Detailsitem);
								let routes = getCurrentPages();
								var url =
									`/st_mes_zs/device/spotcheckDetail?Detailsitem=${JSON.stringify(Detailsitem)}`
								let obj = {
									path: 'manual',
									Name: '设备点检详情',
									APPFunID: '设备点检详情',
									length: routes.length + 1,
									url: url
								}
								this.GonavigateTo(obj, url)
							}
						})

					}
				})
			},
			GetMJPeriod() {
				var that = this
				that.Request.request('getPeriod', {
					FType: 'SBDJ'
				}, (ret) => {
					ret.data.ds.map(item => {
						item.name = item.PeriodTypeName
						item.value = item.PeriodType
					})
					that.PeriodTypeList = ret.data.ds
				})
			},
			getList() {
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},


			changePopup() {

			},
			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},

			pickerConfirm(e) {
				let that = this;
				var time = getFormatDateTime(e.value)
				var formDefaults = {}
				if (this.TimeName == '开始') {

					formDefaults = {
						FBDate: time
					}
				} else {
					formDefaults = {
						FEDate: time
					}
				}
				that.assignFormData(that.FormData, formDefaults);
				this.show = false
			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {
				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			add() {
				let that = this;
				let routes = getCurrentPages();
				var url = '/st_mes_zs/device/spotcheckDetail'
				let obj = {
					path: 'manual',
					Name: '设备点检详情',
					APPFunID: '设备点检详情',
					length: routes.length + 1,
					url: url
				}
				this.GonavigateTo(obj, url)
			},
			// 创建表单赋值工具函数
			assignFormData(target, defaults = {}) {
				var that = this;
				Object.keys(defaults).forEach(key => {
					target[key] = defaults[key];
				});
				that.$forceUpdate();
			},
			getTarget(arrayName, status) {
				// 1. 根据arrayName获取对应数组
				const targetArray = this[arrayName];
				// 2. 在目标数组中查找
				const obj = targetArray.find(item => item.value === status);
				return obj.name || '';
			},
			queryList(pageNo) {
				let that = this;
				that.FormData.PageIndex = pageNo


				try {
					console.log(that.FormData);
					that.Request.request('getCheckuEquipmentList', that.FormData, (ret) => {
						console.log(ret);
						that.showMLoading = false
						if (ret.status == 200) {
							ret.data.ds.map((item) => {
								// item.BillDate = formatDateTime(item.BillDate)
								item.PeriodTypeName = that.getTarget('PeriodTypeList', item.PeriodType)
								item.BillDate = item.BillDate ? item.BillDate.split('T')[0] : ''
							})
							that.$refs.paging.complete(ret.data.ds);
						}
					}, (err) => {})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}


			},
			itemClick(item, index) {
				// this.hoveredIndex = index;
				let that = this;
				let routes = getCurrentPages();
				var url = `/st_mes_zs/device/spotcheckDetail?CheckupEquipmentID=${item.CheckupEquipmentID}`
				let obj = {
					path: 'manual',
					Name: '设备点检详情',
					APPFunID: '设备点检详情',
					length: routes.length + 1,
					url: url
				}
				this.GonavigateTo(obj, url)
			},
			GonavigateTo(obj, url) {
				let that = this;
				var initData = JSON.parse(JSON.stringify(that.$store.state.GuyueModule.ModuleData_select));
				var parentData = initData[initData.length - 1]
				parentData.url = `/st_mes_zs/device/spotcheck?Dispatchitem=${JSON.stringify(that.FormData)}`;
				that.$modifyParentData(parentData);
				this.$safeNavigate({
					targetUrl: url, // 目标页面路径
					payload: obj, // 要传递的数据
				});
			},
			async AddModuleData_select(res) {
				try {
					const backDelta = await this.GetModuleData_select(res);
				} catch (error) {
					console.error('返回操作失败:');
				}
			},
			changeLevel(list, data, row) {
				getchangeLevel(data)
			},
			clearHovered() {
				this.hoveredIndex = null; // 清除hover的tr索引  
			},
		},


		onNavigationBarButtonTap(e) {
			if (this.showLeft) {
				this.$refs.showLeft.close()
			} else {
				this.$refs.showLeft.open()
			}
		},
		// app端拦截返回事件 ，仅app端生效
		onBackPress() {
			if (this.showRight || this.showLeft) {
				this.$refs.showLeft.close()
				this.$refs.showRight.close()
				return true
			}
		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";
</style>